#!/bin/bash
#
# Author:   Joshua Chen <iesugrace@gmail.com>
# Date:     2017-08-02 18:19:53
# Location: Shenzhen
# Desc: feed the proxy database with new.
#

insert() {
    local sql
    read sql
    sql="insert into proxy values $sql"
    sqlite3 "$db" "$sql"
}

fetch() {
    for fetcher in proxy_fetcher/*
    do
        test -x $fetcher && $fetcher
    done | sort -u
}

work() {
    local newip sql

    newip=$(mktemp)
    fetch > $newip

    flock $lock_fd

    sql="delete from proxy where status=0"
    sqlite3 "$db" "$sql"

    sql="select address from proxy"
    diff <(sqlite3 "$db" "$sql" | sort) $newip | grep '>'       \
            | sed -r -e 's/> //' \
            | grep -v '^$' \
            | sed -r "s/^(.*)$/\('\1',0,1\),/"   \
            | tr -d '\n' | sed 's/,$//'                         \
            | insert

    flock -u $lock_fd

    rm $newip -f
}

create_db() {
    rm -f "$db"
    sql="create table proxy (address text primary key, time integer, status integer)"
    sqlite3 "$db" "$sql"
}

db="/tmp/proxy.db"
interval=120
lock_file="/tmp/proxy_mod_lock"

wd=$(dirname $0)
cd $wd

#
# Create a new database if it's not a resuming
#
if test "$1" != resume
then
    create_db
fi

exec {lock_fd}>"$lock_file"
while true
do
    work
    sleep $interval
done
